import java.sql.*;

public class ConnectTool {
    /* 获取数据库连接方法 */
    public static Connection getConnect(String dbName) throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        // 创建连接
        String url = "jdbc:mysql://localhost:3306/" + dbName;
        String username = "root";
        String password = "123456";
        Connection connection = DriverManager.getConnection(url, username, password);
        return connection;
    }

    /* 获取连接预处理方法 */
    public static Statement getStatement(Connection conn) throws Exception {
        // 创建stmt对象
        Statement statement = conn.createStatement();
        return statement;
    }

    /* 关闭连接方法 */
    public static void CloseDb(Connection conn, Statement st, ResultSet rs) throws Exception {
        // 按顺序判断是否存在并关闭连接
        if (null != rs) {
            rs.close();
        }
        if (null != st) {
            st.close();
        }
        if (null != conn) {
            conn.close();
        }
    }
}
